﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace HypermarketSerialPort._1_Model
{
    class LogInManager
    {
        static private string MSG_WELCOME = "Welcome ";
        static private string MSG_CASHIERID_ERROR = "Sorry, wrong User ID or Password, please try again!";
        static private string MSG_MACHINE_DUPLICATE = "Cashier Register is being used";

        private static LogInManager mInstance;
        private DBManager mDBManager;

        #region LogInManager 
        //only create one instance of LogInManager
        public static LogInManager getInstance()
        {
            if (mInstance == null)
            {
                mInstance = new LogInManager();
            }
            return mInstance;
        }
        #endregion


        #region Constructor
        private LogInManager()
        {
            mDBManager = DBManager.getInstance();
        }
        #endregion


        #region Operation Login
        //received a list of string from controller
        public string OperationLogin(List<string> receivedInfoList, Dictionary<string, CashierRegister> cashiersDict)
        {
            if (receivedInfoList.Count() == Constant.THREE)
            {
                //string machineID = (int.Parse(s_receivedInform.Substring(1, 1))).ToString().PadLeft(4, '0');
                string cashierID = receivedInfoList[Constant.ONE];
                string cashierPassword = receivedInfoList[Constant.TWO];
                string machineID = receivedInfoList[Constant.ZERO];
                //string endCheckByte = s_receivedInform.Substring(11, 1);
                if (cashiersDict.ContainsKey(machineID))
                {
                    return MSG_MACHINE_DUPLICATE;
                }
                else if (!CheckValidLogin(cashierID, cashierPassword))
                {
                    return MSG_CASHIERID_ERROR;
                }
                else
                {
                    CashierRegister cashier = new CashierRegister(cashierID, machineID);
                    cashiersDict.Add(machineID, cashier);
                    return MSG_WELCOME + cashierID + Constant.NEWLINE;
                 }
            }
            else
            {
                return Constant.PROGRAM_ERROR;
            }
        }
        #endregion 


        #region Check Valid Login Assessment
        private bool CheckValidLogin(string cashierID, string cashierPassword)
        {
            DataTable cashierTable = mDBManager.getTable(Constant.CASHIER_TABLE);
            string query = Constant.CASHIER_ID + Constant.EQUAL + Constant.SINGAL_QUOTE + cashierID +
                            Constant.SINGAL_QUOTE + Constant.AND + Constant.CASHIER_PASSWORD + 
                            Constant.EQUAL + Constant.SINGAL_QUOTE + cashierPassword + Constant.SINGAL_QUOTE;
            DataRow[] row = cashierTable.Select(query);
            if (row.Count() == 0)
            {
                return false;
            }
            else
            {
                //string output = MSG_WELCOME + cashierID + Constant.NEWLINE;
                return true;
            }
        }
        #endregion 

    }
}
